<?php defined('BASEPATH') OR exit('No direct script access allowed');

class diagnosis extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
		if (!$this->ion_auth->logged_in())
		{
			//redirect them to the login page
			redirect('auth/login', 'refresh');
		}
		elseif (!$this->ion_auth->is_admin())
		{
			//redirect them to the home page because they must be an administrator to view this	
			redirect('/', 'refresh');
		}
			$this->load->library('form_validation');
		$this->load->model('diagnosis_model');
        $this->load->library('datatables');	
        $this->load->library('email');		
	}

	//redirect if needed, otherwise display the user list
	function all_diagnosis()
	{
		 $meta['page_title'] = 'ALL Diagnosis';
		 $this->load->view('common/header',$meta);
	     $this->load->view('diagnosis/all_diagnosis');
		 $this->load->view('common/footer');		
	}
	function get()
		{
		echo $this->diagnosis_model->get_view();
		
		}
		function nonwarranty_get()
		{
		echo $this->diagnosis_model->nonwarranty_view();
		
		}
		function get_dispatch()
		{
		echo $this->diagnosis_model->get_disp_view();
		
		}
	function nonwarranty_dispatch()
		{
		echo $this->diagnosis_model->nonwarranty_disp_view();
		
		}
	function dispatch_diagnosis()
	{
	 $meta['page_title'] = 'ALL Dispatch Diagnosis';
		 $this->load->view('common/header',$meta);
	     $this->load->view('diagnosis/dispatch_diagnosis');
		 $this->load->view('common/footer');	
	
	}
	public function get_docket($id)
	{
	$this->data['case_id']=$id;
	$this->data['docket']=$this->diagnosis_model->get_docket_detail($id)->row();
	$case_no=$this->diagnosis_model->get_case_n($id)->row();
	$this->data['case_no']=$case_no->case_no;
	$this->data['hp_case']=$case_no->hp_case;
	$this->data['docket_no'] = array(
			'name'  => 'docket_no',
			'id'    => 'docket_no',
			'type'  => 'text',
			);
	     $meta['page_title'] = 'Docket No';
		 $this->load->view('common/header',$meta);
	     $this->load->view('diagnosis/get_docket',$this->data);
		 $this->load->view('common/footer');	
		 }
	public function get_part($id)
	{
	$this->data['case_id']=$id;
	$this->data['order_exist']=$this->diagnosis_model->chk_order($id)->row();
	$this->data['supplier']=$this->diagnosis_model->get_supplier();
	$case_no=$this->diagnosis_model->nonwarranty_case_n($id)->row();
	$this->data['part']=$this->diagnosis_model->get_part_detail($id);
	$this->data['case_no']=$case_no->case_no;
    $data['max']=$this->diagnosis_model->get_max_order()->row();

	$max_id=$data['max']->id+1;
	$date=date('M');
	$order=strtoupper('ORD/'.$date.'/'.$max_id);
      $this->data['order']=$order;
    $meta['page_title'] = 'Place Order';
    $this->load->view('common/header',$meta);
    $this->load->view('nonwarranty/place_order',$this->data);
    $this->load->view('common/footer');		
	}
	public function docket_no()
	{
	$case_id=$_REQUEST['case_id'];
	$case_no=$_REQUEST['case_no'];
	$data=$this->diagnosis_model->get_email($case_id)->row();	
	$company=$this->diagnosis_model->get_company()->row();
	
	$email=$this->input->post('email');
	$from=$company->email;
	$to=$data->email;
    if($email==1)
	{
	$this->load->library('email');
    $this->email->from($from, $company->name);
    $this->email->to($to); 
    $this->email->cc($from);  
    $this->email->subject('Diagnosis Completed for your Case No'. $case_no);
    $this->email->message('Diagnosis have been completed');	
    $this->email->send();	
	}
	
	$insert=array('case_id'=>$_REQUEST['case_id'],
	             'docket_no'=>$_REQUEST['docket_no'],
				 'dispatch_date'=>$_REQUEST['dispatch_date'],
				 'expected_date'=>$_REQUEST['expected_date'],);
	
	
	$this->diagnosis_model->enter_docket($insert);
	$this->diagnosis_model->update_case_status($_REQUEST['case_id']);
	redirect('admin/diagnosis/dispatch_diagnosis');
	}
function create_supplier()
	{
	
		$this->data['page_title'] = "Create Supplier";
		$meta['page_title'] = "Create Supplier";
		$this->form_validation->set_rules('supplier_name', 'Supplier Name', 'required|xss_clean');
	    $this->form_validation->set_rules('mobile', 'Phone Number', 'required|xss_clean');
		  $this->form_validation->set_rules('landline', 'Landline Number', 'required|numeric');
		$this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
		$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
		if ($this->form_validation->run() == true)
		{
			$insert=array('name'=>$_REQUEST['supplier_name'],
			              'address'=>$_REQUEST['address'],
	                       'mobile'=>$_REQUEST['mobile'],
						   'email'=>$_REQUEST['email'],
					        'landline'=>$_REQUEST['landline'],);
		
		$this->diagnosis_model->create_supplier($insert);
		redirect('/admin/diagnosis/view_supplier');
		}
		else
		{
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['supplier_name'] = array(
				'name'  => 'supplier_name',
				'id'    => 'supplier_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('supplier_name'),
			);
			
			
		
			$this->data['mobile'] = array(
				'name'  => 'mobile',
				'id'    => 'mobile',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('mobile'),
			);
			$this->data['landline'] = array(
				'name'  => 'landline',
				'id'    => 'landline',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('landline'),
			);

			$this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
			
			$this->data['address'] = array(
				'name'  => 'address',
				'id'    => 'address',
				'type'  => 'textarea',
				'value' => $this->form_validation->set_value('address'),
			);


			$this->load->view('common/header', $meta);
			
			$this->load->view('nonwarranty/supplier', $this->data);
			$this->load->view('common/footer');
		}
	}
		
public function view_supplier()
{
$meta['page_title'] = "View Supplier";
$this->load->view('common/header',$meta);
 $this->load->view('nonwarranty/view_supplier');
 $this->load->view('common/footer');
}	
public function supplier()
{
echo $this->diagnosis_model->supplier_view();
}	
public function edit_supplier($id)
{ 
	   $this->data['page_title'] = "Edit  Supplier";
		$meta['page_title'] = "Edit Supplier";
		$supplier=$this->diagnosis_model->get_supplier_detail($id)->row();
		$this->form_validation->set_rules('supplier_name', 'Supplier Name', 'required|xss_clean');
	    $this->form_validation->set_rules('mobile', 'Phone Number', 'required|xss_clean');
		  $this->form_validation->set_rules('landline', 'Landline Number', 'required|xss_clean');
		$this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
		$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');

	  $this->data['sid']=$id;
		if ($this->form_validation->run() == true)
		{ 
	
			$insert=array('name'=>$_REQUEST['supplier_name'],
			              'address'=>$_REQUEST['address'],
	                       'mobile'=>$_REQUEST['mobile'],
						   'email'=>$_REQUEST['email'],
					        'landline'=>$_REQUEST['landline'],);
				
		$this->diagnosis_model->update_supplier($insert,$id);
		redirect('/admin/diagnosis/view_supplier');
		}
		
		else
		{
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['supplier_name'] = array(
				'name'  => 'supplier_name',
				'id'    => 'supplier_name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('supplier_name',$supplier->name),
			);
			$this->data['id'] = array(
				'name'  => 'id',
				'id'    => 'id',
				'type'  => 'hidden',
				'value' => $this->form_validation->set_value('id',$supplier->id),
			);
			
		
			$this->data['mobile'] = array(
				'name'  => 'mobile',
				'id'    => 'mobile',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('mobile',$supplier->mobile),
			);
			$this->data['landline'] = array(
				'name'  => 'landline',
				'id'    => 'landline',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('landline',$supplier->landline),
			);

			$this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('email',$supplier->email),
			);
			
			$this->data['address'] = array(
				'name'  => 'address',
				'id'    => 'address',
				'type'  => 'textarea',
				'value' => $this->form_validation->set_value('address',$supplier->address),
			);
			$this->load->view('common/header', $meta);
			$this->load->view('nonwarranty/edit_supplier', $this->data);
			$this->load->view('common/footer');
		}
}	
public function delete_supplier($id)
{
$this->diagnosis_model->delete_supplier($id);
redirect('/admin/diagnosis/view_supplier');
}
public function view_assigned_diagnosis()
{
$id=$this->session->userdata('user_id');
	$meta['page_title'] = 'New Assigned Diagnosis';
	$this->load->view('common/header',$meta);		
	$this->load->view('diagnosis/assigned_diagnosis');
	$this->load->view('common/footer');
}
public function ongoing_diagnosis_view()
{
 $meta['page_title'] = 'Current Diagnosis';
	$this->load->view('common/header',$meta);		
	$this->load->view('diagnosis/current_diagnosis');
	$this->load->view('common/footer');


}
public function completed_diagnosis_view()
{
 $meta['page_title'] = 'Dispatched Diagnosis';
	$this->load->view('common/header',$meta);		
	$this->load->view('diagnosis/dispatch_diagnosis1');
	$this->load->view('common/footer');



}
function getall()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->get_view_admin($id);
		print_r($at);
		
		}
		function nonwarranty_getall()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->nonwarranty_view_admin($id);
		print_r($at);
		
		}	
	function getcurrent()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->get_current_view($id);
		print_r($at);
		
		}
		function nonwarranty_getcurrent()
		{ 
	   $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->nonwarranty_current_view_admin($id);
		print_r($at);
		
		}	
		
public function getdispatch()
{
  $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->get_dispatch_view_admin($id);
		print_r($at); 
}
public function nonwarranty_getdispatch()
{
  $id=$this->session->userdata('user_id');
		$at=$this->diagnosis_model->nonwarranty_dispatch_view_admin($id);
		print_r($at); 
}		
		
		
		
}
	